﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Configuration;
using System.Web.Configuration;

namespace SPWP.OpinionPoll
{
    public class PollVoteList
    {
        private string listLocation = WebConfigurationManager.AppSettings["listpoll"].ToString();

        public string SPPollSite { get; set; }
        public string SPPollList { get; set; }
        public string SPPollListAnswer { get; set; }
        public string SPPollListVote { get; set; }

        string spsite;

        public PollVoteList(string splist, string splistanswer)
        {
            spsite = SPPollSite;
            SPPollList = splist;
            SPPollListAnswer = splistanswer;
        }

        public PollVoteList(string spvotelist)
        {
            SPPollListVote = spvotelist;
        }

        public PollVoteList()
        {
            
        }
        
        /// <summary>
        /// Get Poll Results
        /// </summary>
        /// <param name="pollID">Poll ID to be viewed</param>
        /// <returns>One poll details</returns>
        public List<PollVote> GetResult(int poll)
        {
            List<PollVote> listVote = new List<PollVote>();

            try
            {
                using (SPSite objSite = new SPSite(listLocation))
                {
                    using (SPWeb objWeb = objSite.OpenWeb())
                    {
                        // retrieve survey question
                        SPQuery query = new SPQuery();
                        query.ViewFields = @"<FieldRef Name=""AnswerPicked"" /><FieldRef Name=""Title"" /><FieldRef Name=""VoterEmail"" />";
                        query.Query = @"<Query><Where><IsNotNull><FieldRef Name=""Title"" /></IsNotNull></Where></Query>";

                        string title = "Poll Votes-" + poll.ToString();

                        SPList objList = objWeb.Lists[title];
                        SPListItemCollection filteredItems = objList.GetItems(query);
                
                        foreach (SPListItem item in filteredItems)
                        { 
                            PollVote voteItem = new PollVote();

                            voteItem.Title = item["Title"].ToString();
                            voteItem.AnswerPicked = item["AnswerPicked"].ToString();
                            voteItem.IPAddress = item["VoterEmail"].ToString();
                            voteItem.ID = Convert.ToInt32(item["ID"].ToString());
                            voteItem.Created = DateTime.Parse(item["Created"].ToString());

                            listVote.Add(voteItem);
                        }
                    }
                }


            }
            catch (Exception ex)
            {
                 //String.Format("{0}: {1}", ErrorMessage.Error, ex.Message);
            }

            return listVote;

        }

        /// <summary>
        /// Delete vote
        /// </summary>
        /// <param name="delID">Vote ID to be deleted</param>
        /// <returns>Status message</returns>
        public string DeleteVote(int delID, int poll)
        {
            try
            {
                using (SPSite objSite = new SPSite(listLocation))
                {
                    using (SPWeb objWeb = objSite.OpenWeb())
                    {
                        bool unSafeUpdate = objWeb.AllowUnsafeUpdates;
                        objWeb.AllowUnsafeUpdates = true;

                        SPQuery objQuery = new SPQuery();
                        string ID = delID.ToString();  // hidden field
                        objQuery.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + ID + "</Value></Eq></Where>";

                        string title = "Poll Votes-" + poll.ToString();

                        SPList objList = objWeb.Lists[title];
                        SPListItem objItem = objList.GetItemById(int.Parse(ID));
                        objItem.Delete();

                        objWeb.AllowUnsafeUpdates = unSafeUpdate;

                        return "Vote successfully deleted";
                    }

                }
            }
            catch (Exception ex)
            {
                return String.Format("{0}: {1}", "Error", ex.Message);
            }

        }
    }
}